#include <iostream>

#include <curl/curl.h>
#include "Adresse.h"
#include "FromXMLToGoogleMapHTTPRequest.h"

FromXMLToGoogleMapHTTPRequest::FromXMLToGoogleMapHTTPRequest(std::string id) : LwSaxParser()
{
    this->id = "00" + id;

    request = "";
    listeAdresses = "";
    state = OTHER;
}

FromXMLToGoogleMapHTTPRequest::~FromXMLToGoogleMapHTTPRequest() {

}

std::string FromXMLToGoogleMapHTTPRequest::getGoogleMapHttpRequest() {
    request = "origins=" + std::string(curl_easy_escape(NULL,listeAdresses.c_str(),0)) + "&destinations=" + std::string(curl_easy_escape(NULL,listeAdresses.c_str(),0));
    return request;
}

std::string FromXMLToGoogleMapHTTPRequest::findAttribute(const AttributeList& attributeList, std::string attributeName) {
    std::string resultat = "";
    xmlpp::SaxParser::AttributeList::const_iterator iter = attributeList.begin();
    while ((iter != attributeList.end()) && (iter->name != attributeName)) {
        iter++;
    }
    if (iter != attributeList.end()) {
        resultat = iter->value;
    }
    return resultat;
}

// A compléter en implémentant les méthodes à redéfinir de LwSaxParser

void FromXMLToGoogleMapHTTPRequest::parseDocument(char * filename) {
 try
    {
        std::cout << "Lecture du fichier " << filename << " ligne par ligne..." << std::endl;
        std::string line;
        std::ifstream inputStream(filename);

        while (std::getline(inputStream, line)) {
            parse_chunk(line);
        }
        finish_chunk_parsing();
        std::cout << "Fin de la lecture du fichier " << filename << "." << std::endl;
      }
      catch(const xmlpp::exception& ex)
       {
           std::cout << "libxml++ exception: " << ex.what() << std::endl;
       }
}

void FromXMLToGoogleMapHTTPRequest::on_start_element(const Glib::ustring& name, const AttributeList& attributes)
{
  if(name == "adresse")
  {
  	state = ADRESSE;
  }
  else if(name == "etage"){
  	state = ETAGE;
  }
  else if(name == "numero"){
  	state = NUMERO;
  }
  else if(name == "rue"){
  	state = RUE;
  }
  else if(name == "ville"){
  	state = VILLE;
  }
  else if(name == "codePostal"){
  	state = CODEPOSTAL;
  }

}

void FromXMLToGoogleMapHTTPRequest::on_end_element(const Glib::ustring& name)
{
  if(name == "adresse")
  {
  	listeAdresses += "|";
  	state = PATIENT;
  }
  else if(name == "etage"){
  	state = ADRESSE;
  }
  else if(name == "numero"){
  	state = ADRESSE;
  }
  else if(name == "rue"){
  	state = ADRESSE;
  }
  else if(name == "ville"){
  	state = ADRESSE;
  }
  else if(name == "codePostal"){
  	state = ADRESSE;
  }
}

void FromXMLToGoogleMapHTTPRequest::on_characters(const Glib::ustring& text)
{
  adresseCourante = new Adresse();
  if(state == NUMERO){
  	if(text != ""){
        	adresseCourante->setNumero(text);
        	listeAdresses += text + "-";
  	}
  }
  else if(state == RUE){
  	adresseCourante->setRue(text);
  	listeAdresses += text + "-";
  }
  else if(state == VILLE){
  	adresseCourante->setVille(text);
  	listeAdresses += text + "-";
  }
  else if(state == CODEPOSTAL){
  	adresseCourante->setCodePostal(text);
  	listeAdresses += text + "-";
  }
}

void FromXMLToGoogleMapHTTPRequest::on_start_document() {
	std::cout << "Appel de la méthode: on_start_document()" << std::endl;
	state = START;
}

void FromXMLToGoogleMapHTTPRequest::on_end_document() {
	std::cout << "Appel de la méthode: on_end_document()" << std::endl;
}

